package cn.csc.module.pay.service.jzborder;


import cn.csc.framework.jzb.core.JzbClient;
import cn.csc.framework.jzb.vo.JzbResponse;
import cn.csc.module.pay.controller.app.jzb.vo.*;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.Gson;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @Author：云隐
 * @Date：2023/5/6 9:37
 */
@Slf4j
@Service
public class JzbThirdService {

    @Resource
    JzbClient jzbClient;

    private static final Gson gson = new Gson();

    /**
     * 登记挂账(支持撤销) KFEJZB6139
     * @param reqVO 登记挂账参数
     * @return
     */
    public JzbResponse registerBillSupportWithdraw(RegisterBillSupportWithdrawReqVO reqVO){
        log.info("[registerBillSupportWithdraw]{}", reqVO);
        JSONObject jsonObject = new JSONObject();

        //订单号
        jsonObject.put("OrderNo", reqVO.getOrderNo());
        //子账户
        jsonObject.put("SubAcctNo", reqVO.getSubAcctNo());
        //交易网会员代码
        jsonObject.put("TranNetMemberCode", reqVO.getTranNetMemberCode());
        //交易费用
        jsonObject.put("TranFee", reqVO.getTranFee());
        //挂账金额
        jsonObject.put("SuspendAmt", reqVO.getSuspendAmt());
        //备注
        jsonObject.put("Remark", reqVO.getRemark());
        //保留域1
        jsonObject.put("ReservedMsgOne", reqVO.getReservedMsgOne());
        //保留域2
        jsonObject.put("ReservedMsgTwo", reqVO.getReservedMsgTwo());
        //手续费
        jsonObject.put("ReservedMsgThree", reqVO.getReservedMsgThree());
        JzbResponse response = jzbClient.send(jsonObject, "KFEJZB6139", "/V1.0/RegisterBillSupportWithdraw");
        log.info("[registerBillSupportWithdraw]response={}", response);
        return response;
    }



    /**
     * 登记挂账撤销 KFEJZB6140
     * @param reqVO 登记挂账撤销参数
     * @return
     */
    public JzbResponse revRegisterBillSupportWithdraw(RevRegisterBillSupportWithdrawReqVO reqVO){
        log.info("[revRegisterBillSupportWithdraw]{}", reqVO);
        JSONObject jsonObject = new JSONObject();
        //订单号
        jsonObject.put("OldOrderNo", reqVO.getOldOrderNo());
        //子账户
        jsonObject.put("SubAcctNo", reqVO.getSubAcctNo());
        //交易网会员代码
        jsonObject.put("TranNetMemberCode", reqVO.getTranNetMemberCode());
        //交易费用
        jsonObject.put("TranFee", reqVO.getTranFee());
        //撤销金额
        jsonObject.put("CancelAmt", reqVO.getCancelAmt());
        //备注
        jsonObject.put("Remark", reqVO.getRemark());
        //保留域1
        jsonObject.put("ReservedMsgOne", reqVO.getReservedMsgOne());
        //保留域2
        jsonObject.put("ReservedMsgTwo", reqVO.getReservedMsgTwo());
        //手续费
        jsonObject.put("ReservedMsgThree", reqVO.getReservedMsgThree());
        JzbResponse response = jzbClient.send(jsonObject, "KFEJZB6140", "/V1.0/RevRegisterBillSupportWithdraw");
        log.info("[revRegisterBillSupportWithdraw]response={}", response);
        return response;
    }


    /**
     * 登记行为记录信息 KFEJZB6244
     * @param reqVO 登记行为记录信息参数
     * @return
     */
    public JzbResponse registerBehaviorRecordInfo(RegisterBehaviorRecordInfoReqVO reqVO){
        log.info("[registerBehaviorRecordInfo]{}", reqVO);
        JSONObject jsonObject = new JSONObject();

        //功能标志
        jsonObject.put("FunctionFlag", reqVO.getFunctionFlag());
        //子账户
        jsonObject.put("SubAcctNo", reqVO.getSubAcctNo());
        //交易网会员代码
        jsonObject.put("TranNetMemberCode", reqVO.getTranNetMemberCode());
        //操作时间
        jsonObject.put("OpClickTime", reqVO.getOpClickTime());
        //ip地址
        jsonObject.put("IpAddress", reqVO.getIpAddress());
        //MAC地址
        jsonObject.put("MacAddress", reqVO.getMacAddress());
        //签约
        jsonObject.put("SignChannel", reqVO.getSignChannel());
        //保留域1
        jsonObject.put("ReservedMsgOne", reqVO.getReservedMsgOne());
        //保留域2
        jsonObject.put("ReservedMsgTwo", reqVO.getReservedMsgTwo());
        JzbResponse response = jzbClient.send(jsonObject, "KFEJZB6244", "/V1.0/RegisterBehaviorRecordInfo");
        log.info("[registerBehaviorRecordInfo]response={}", response);
        return response;
    }


    /**
     * 分账原生接口 KFEJZB6271
     * @param reqVO 分账请求参数
     * @param seq 交易流水号
     * @return
     */
    public JzbResponse subAccountQuest(SubAccountQuestReqVO reqVO, String seq){
        log.info("[subAccountQuest]{}", reqVO);
        JSONObject jsonObject = new JSONObject();

        //原支付订单的子单号
        jsonObject.put("OldPaySubOrderNo", reqVO.getOldPaySubOrderNo());
        //原支付订单所属日期
        jsonObject.put("OldPayOrderDate", reqVO.getOldPayOrderDate());
        //分账转出子账户
        jsonObject.put("OutSubAcctNo", reqVO.getOutSubAcctNo());
        //总交易金额
        jsonObject.put("TranTotalAmt", reqVO.getTranTotalAmt());
        //订单数量 1到30
        jsonObject.put("OrdersCount", reqVO.getOrdersCount());
        // 分账转入子账户数组
        jsonObject.put("TranItemArray", JSON.parseArray(gson.toJson(reqVO.getTranItemArray())) );
        //保留域1
        jsonObject.put("ReservedMsgOne", reqVO.getReservedMsgOne());
        //保留域2
        jsonObject.put("ReservedMsgTwo", reqVO.getReservedMsgTwo());

        log.info("[subAccountQuest]OldPaySubOrderNo={}, seq={}", reqVO.getOldPaySubOrderNo(), seq);
        JzbResponse response = jzbClient.send(jsonObject, "KFEJZB6271", "/V1.0/SubAccountQuest", seq);
        log.info("[subAccountQuest]response={}", response);
        return response;
    }

    /**
     * 会员提现 KFEJZB6033
     * @param reqVO 会员提现请求
     * @param seq 交易流水号
     * @return
     */
    public JzbResponse membershipWithdrawCash(MembershipWithdrawCashReqVO reqVO, String seq) {
        JSONObject jsonObject = new JSONObject();

        //交易网名称
        jsonObject.put("TranWebName", jzbClient.getJzbProperties().getMrchCode());
        //会员证件类型
        jsonObject.put("MemberGlobalType", reqVO.getMemberGlobalType());
        //子账户
        jsonObject.put("SubAcctNo", reqVO.getSubAcctNo());
        //会员证件号码
        jsonObject.put("MemberGlobalId", reqVO.getMemberGlobalId());
        //交易网会员代码
        jsonObject.put("TranNetMemberCode", reqVO.getTranNetMemberCode());
        //会员名称
        jsonObject.put("MemberName", reqVO.getMemberName());
        //提现账号 银行卡
        jsonObject.put("TakeCashAcctNo", reqVO.getTakeCashAcctNo());
        //提现账户户名 银行卡姓名
        jsonObject.put("OutAmtAcctName", reqVO.getOutAmtAcctName());
        //币种
        jsonObject.put("Ccy", reqVO.getCcy());
        //提现金额
        jsonObject.put("CashAmt", reqVO.getCashAmt());
        //备注
        jsonObject.put("Remark", reqVO.getRemark());
        //手续费
        jsonObject.put("ReservedMsg", reqVO.getReservedMsg());

        //网络签名
        jsonObject.put("WebSign", reqVO.getWebSign());

        JzbResponse response = jzbClient.send(jsonObject, "KFEJZB6033", "/V1.0/MembershipWithdrawCash", seq);

        return response;
    }

    /**
     * 交易终态查询 KFEJZB6273
     * @param reqVO 交易终态查询参数
     * @return
     */
    public JzbResponse traFinalStatusQuery(TraFinalStatusQueryReqVO reqVO){
        log.info("[traFinalStatusQuery]{}", reqVO);
        JSONObject jsonObject = new JSONObject();
        //原第三方流水号和原见证受理流水号必输其一
        //原第三方流水号
        jsonObject.put("OldThirdSeqNo", reqVO.getOldThirdSeqNo());
        //原见证受理流水号
        jsonObject.put("OldJzbAcceptSeqNo", reqVO.getOldJzbAcceptSeqNo());
        String seq = jzbClient.getCnsmrSeqNo();
        JzbResponse response = jzbClient.send(jsonObject, "KFEJZB6273", "/V1.0/TraFinalStatusQuery", seq);
        return response;
    }

    /**
     * 冻结充值 KFEJZB6267
     * @param reqVO 冻结充值参数
     * @return
     */
    public JzbResponse thirdFreezeRecharge(ThirdFreezeRechargeReqVO reqVO, String seq){
        log.info("[thirdFreezeRecharge]{}", reqVO);
        JSONObject jsonObject = new JSONObject();

        jsonObject.put("PayChannelType", reqVO.getPayChannelType());
        //支付渠道所分配的商户号：即市场在第三方支付渠道的商户号
        jsonObject.put("PayChannelAssignMerNo", reqVO.getPayChannelAssignMerNo());
        //支付订单所属日期 20211009
        jsonObject.put("PayOrderDate", reqVO.getPayOrderDate());
        //即第三方支付渠道产生的支付订单号
        jsonObject.put("TotalOrderNo", reqVO.getTotalOrderNo());
        //总交易金额 -- 总的充值金额（渠道类型为1111-其他时，金额必须为0.00）
        jsonObject.put("TranTotalAmt", reqVO.getTranTotalAmt());
        //订单数量 1到30
        jsonObject.put("OrdersCount", reqVO.getOrdersCount());
        //交易信息数组
        jsonObject.put("TranItemArray", JSON.parseArray(gson.toJson(reqVO.getTranItemArray())));
        //保留域1
        jsonObject.put("ReservedMsgOne", reqVO.getReservedMsgOne());
        //保留域2
        jsonObject.put("ReservedMsgTwo", reqVO.getReservedMsgTwo());
        JzbResponse response = jzbClient.send(jsonObject, "KFEJZB6267", "/V1.0/ThirdFreezeRecharge", seq);
        return response;

    }


    /**
     * 冻结充值撤销 KFEJZB6268
     * @param reqVO 冻结充值撤销参数
     * @return
     */
    public JzbResponse thirdFreezeRechargeCancel(ThirdFreezeRechargeCancelReqVO reqVO, String seq) {
        log.info("[thirdFreezeRechargeCancel]{}", reqVO);
        JSONObject jsonObject = new JSONObject();

        jsonObject.put("PayChannelType", reqVO.getPayChannelType());
        //支付渠道所分配的商户号：即市场在第三方支付渠道的商户号
        jsonObject.put("PayChannelAssignMerNo", reqVO.getPayChannelAssignMerNo());
        //支付订单所属日期 20211009
        jsonObject.put("OldPayOrderDate", reqVO.getOldPayOrderDate());
        //即第三方支付渠道产生的支付订单号
        jsonObject.put("TotalOrderNo", reqVO.getTotalOrderNo());
        //总交易金额 -- 总的充值金额（渠道类型为1111-其他时，金额必须为0.00）
        jsonObject.put("TranTotalAmt", reqVO.getTranTotalAmt());
        //订单数量 1到30
        jsonObject.put("OrdersCount", reqVO.getOrdersCount());
        //交易信息数组
        jsonObject.put("TranItemArray", JSON.parseArray(gson.toJson(reqVO.getTranItemArray())));
        //保留域1
        jsonObject.put("ReservedMsgOne", reqVO.getReservedMsgOne());
        //保留域2
        jsonObject.put("ReservedMsgTwo", reqVO.getReservedMsgTwo());
        JzbResponse response = jzbClient.send(jsonObject, "KFEJZB6268", "/V1.0/ThirdFreezeRechargeCancel", seq);
        log.info("[thirdFreezeRechargeCancel]{}", response);
        return response;
    }


    /**
     * 分账撤销 KFEJZB6272
     * @param reqVO 分账请求参数
     * @return
     */
    public JzbResponse subAccountCancel(SubAccountCancelReqVO reqVO){
        log.info("[subAccountCancel]{}", reqVO);
        JSONObject jsonObject = new JSONObject();

        //原支付订单的子单号
        jsonObject.put("OldPaySubOrderNo", reqVO.getOldPaySubOrderNo());
        //原支付订单所属日期
        jsonObject.put("OldPayOrderDate", reqVO.getOldPayOrderDate());
        //分账转出子账户
        jsonObject.put("OldOutSubAcctNo", reqVO.getOldOutSubAcctNo());
        //总交易金额
        jsonObject.put("TranTotalAmt", reqVO.getTranTotalAmt());
        //订单数量 1到30
        jsonObject.put("OrdersCount", reqVO.getOrdersCount());
        // 分账转入子账户数组
        jsonObject.put("TranItemArray", JSON.parseArray(gson.toJson(reqVO.getTranItemArray())) );
        //保留域1
        jsonObject.put("ReservedMsgOne", reqVO.getReservedMsgOne());
        //保留域2
        jsonObject.put("ReservedMsgTwo", reqVO.getReservedMsgTwo());

        String seq = jzbClient.getCnsmrSeqNo();
        log.info("[subAccountCancel]OldPaySubOrderNo={}, seq={}", reqVO.getOldPaySubOrderNo(), seq);
        JzbResponse response = jzbClient.send(jsonObject, "KFEJZB6272", "/V1.0/SubAccountCancel", seq);
        log.info("[subAccountCancel]{}", response);
        return response;
    }


    /**
     * 查询银行单笔交易状态
     * SingleTransactionStatusQuery
     */
    public JzbResponse singleTransactionStatusQuery(SingleTransactionStatusQueryReqVO reqVO){
        log.info("[singleTransactionStatusQuery]{}", reqVO);
        JSONObject jsonObject = new JSONObject();

        //功能标志 2：会员间交易 3：提现 4：充值
        jsonObject.put("FunctionFlag", reqVO.getFunctionFlag());
        //交易网流水号
        jsonObject.put("TranNetSeqNo", reqVO.getTranNetSeqNo());
        //见证子帐户的帐号
        jsonObject.put("SubAcctNo", reqVO.getSubAcctNo());
        //交易日期
        jsonObject.put("TranDate", reqVO.getTranDate());
        //保留域
        jsonObject.put("ReservedMsg", reqVO.getReservedMsg());

        String seq = jzbClient.getCnsmrSeqNo();
        JzbResponse response = jzbClient.send(jsonObject, "KFEJZB6110", "/V1.0/SubAccountCancel", seq);
        log.info("[singleTransactionStatusQuery]{}", response);
        return response;

    }




}
